package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysNovelCarouselMapper;
import com.ruoyi.system.domain.SysNovelCarousel;
import com.ruoyi.system.service.ISysNovelCarouselService;

/**
 * 轮播列表Service业务层处理
 * 
 * @author ZouYangMing
 * @date 2022-11-08
 */
@Service
public class SysNovelCarouselServiceImpl implements ISysNovelCarouselService 
{
    @Autowired
    private SysNovelCarouselMapper sysNovelCarouselMapper;

    @Autowired
    private RedisCache redisCache;

    private final String SYS_NOVEL_CAROUSEL = "sys_novel_carousel:";

    /**
     * 查询轮播列表
     * 
     * @param id 轮播列表主键
     * @return 轮播列表
     */
    @Override
    public SysNovelCarousel selectSysNovelCarouselById(Long id)
    {
        SysNovelCarousel carousel = redisCache.getCacheObject(SYS_NOVEL_CAROUSEL + id);
        if (carousel == null) {
            //为空执行
            SysNovelCarousel carousel1 = sysNovelCarouselMapper.selectSysNovelCarouselById(id);
            redisCache.setCacheObject(SYS_NOVEL_CAROUSEL + id, carousel1);
            return carousel1;
        }
        return carousel;
    }

    /**
     * 查询轮播列表列表
     * 
     * @param sysNovelCarousel 轮播列表
     * @return 轮播列表
     */
    @Override
    public List<SysNovelCarousel> selectSysNovelCarouselList(SysNovelCarousel sysNovelCarousel)
    {
        return sysNovelCarouselMapper.selectSysNovelCarouselList(sysNovelCarousel);
    }

    /**
     * 新增轮播列表
     * 
     * @param sysNovelCarousel 轮播列表
     * @return 结果
     */
    @Override
    public int insertSysNovelCarousel(SysNovelCarousel sysNovelCarousel)
    {
        sysNovelCarousel.setCreateBy(SecurityUtils.getUsername());
        sysNovelCarousel.setCreateTime(DateUtils.getNowDate());
        int index = sysNovelCarouselMapper.insertSysNovelCarousel(sysNovelCarousel);
        redisCache.setCacheObject(SYS_NOVEL_CAROUSEL + sysNovelCarousel.getId(), sysNovelCarousel);
        return index;
    }

    /**
     * 修改轮播列表
     * 
     * @param sysNovelCarousel 轮播列表
     * @return 结果
     */
    @Override
    public int updateSysNovelCarousel(SysNovelCarousel sysNovelCarousel)
    {
        sysNovelCarousel.setUpdateBy(SecurityUtils.getUsername());
        sysNovelCarousel.setUpdateTime(DateUtils.getNowDate());
        int index = sysNovelCarouselMapper.updateSysNovelCarousel(sysNovelCarousel);
        redisCache.deleteObject(SYS_NOVEL_CAROUSEL + sysNovelCarousel.getId());
        redisCache.setCacheObject(SYS_NOVEL_CAROUSEL + sysNovelCarousel.getId(), sysNovelCarousel);
        return index;
    }

    /**
     * 批量删除轮播列表
     * 
     * @param ids 需要删除的轮播列表主键
     * @return 结果
     */
    @Override
    public int deleteSysNovelCarouselByIds(Long[] ids)
    {
        for (Long id : ids) {
            redisCache.deleteObject(SYS_NOVEL_CAROUSEL + id);
        }
        return sysNovelCarouselMapper.deleteSysNovelCarouselByIds(ids);
    }


    /**
     * 删除轮播列表信息
     * 
     * @param id 轮播列表主键
     * @return 结果
     */
    @Override
    public int deleteSysNovelCarouselById(Long id)
    {
        redisCache.deleteObject(SYS_NOVEL_CAROUSEL + id);
        return sysNovelCarouselMapper.deleteSysNovelCarouselById(id);
    }
}
